package dxunderground.winampController.Client.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import dxunderground.winampController.Client.Client;
import dxunderground.winampController.Client.Playlist;
import dxunderground.winampController.Common.FilterPanel;
import dxunderground.winampController.Common.GUI;
import dxunderground.winampController.Common.StatusPanel;

@SuppressWarnings("serial")
public class clientGUI extends GUI {
	private Client client;
	private Playlist playlist;

	public clientGUI(boolean recentJava) throws JDOMException, IOException {
		if (recentJava) {
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build("/Program Files/Winamp Controller/Client Preferences.xml");

			Element properties = doc.getRootElement();

			this.playlist = new Playlist();
			ButtonActions buttonActions = new ButtonActions(this, playlist);

			listModel = new DefaultListModel();
			list = new JList(listModel);
			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			list.setLayoutOrientation(JList.VERTICAL);

			JScrollPane listScroller = new JScrollPane(list);
			listScroller.setPreferredSize(new Dimension(500, 200));

			JPanel filterPanel = new FilterPanel(this, buttonActions);
			JPanel statusPanel = new StatusPanel(this, buttonActions);

			JPanel centerPanel = new JPanel(new BorderLayout());
			centerPanel.add(listScroller, BorderLayout.CENTER);
			centerPanel.add(filterPanel, BorderLayout.SOUTH);

			// Initialize the server connection
			client = new Client();
			if (client.connection) {
				this.playlist.setClient(client);

				this.setLayout(new BorderLayout());
				this.add(centerPanel, BorderLayout.CENTER);
				this.add(statusPanel, BorderLayout.SOUTH);

				initializePlaylist();

				WindowActions windowActions = new WindowActions(this, properties);
				this.addWindowListener(windowActions);
			} else {
				JPanel failPanel = new JPanel();
				JLabel failure = new JLabel("Could not connect to " + Client.host);
				failPanel.add(failure);
				this.add(failPanel);
				this.pack();
			}
		} else {
			JPanel panel = new JPanel(new BorderLayout());
			panel.add(new JLabel("Update your java."), BorderLayout.NORTH);
			JTextField field = new JTextField("http://www.java.com/en/download/windows_xpi.jsp?locale=en&host=www.java.com:80");
			field.setEditable(false);
			panel.add(field, BorderLayout.SOUTH);
			this.add(panel);
			this.pack();
		}
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(true);
	}

	private void initializePlaylist() {
		client.getPlaylist(this.listModel, this.statusLabel, this.playlist);
	}

	public void quit() {
		this.playlist.quit();
	}

	/**
	 * @param args
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public static void main(String[] args) throws JDOMException, IOException {
		boolean recentJava = true;
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (UnsupportedLookAndFeelException e) {
			recentJava = false;
		} catch (InstantiationException e) {
			recentJava = false;
		} catch (IllegalAccessException e) {
			recentJava = false;
		} catch (ClassCastException e) {
			recentJava = false;
		} catch (ClassNotFoundException e) {
			recentJava = false;
		}
		new clientGUI(recentJava);
	}
}
